package com.mszlu.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mszlu.blog.common.aop.LogAnnotation;
import com.mszlu.blog.common.cache.Cache;
import com.mszlu.blog.dao.mapper.ArticleMapper;
import com.mszlu.blog.dao.mapper.ArticleTagMapper;
import com.mszlu.blog.dao.mapper.TagMapper;
import com.mszlu.blog.dao.pojo.Article;
import com.mszlu.blog.service.ArticleService;
import com.mszlu.blog.service.ThreadService;
import com.mszlu.blog.vo.ArticleVo;
import com.mszlu.blog.vo.Result;
import com.mszlu.blog.vo.params.ArticleParam;
import com.mszlu.blog.vo.params.PageParams;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

//json数据进行交互
@RestController
@RequestMapping("articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Resource
    private ArticleMapper articleMapper;
    @Autowired
    private ThreadService threadService;



//    @Resource
//    private  Article article;
    /**
     * 首页 文章列表
//     * @param pageParams
//     * @return
     */
    @PostMapping("list")
    //加上此注解 代表要对此接口记录日志
    @LogAnnotation(module="文章",operator="获取文章列表")
//    @Cache(expire = 5 * 60 * 1000,name = "listArticle")
    public Result listArticle(@RequestBody PageParams pageParams){
        int i = 1;

        return articleService.listArticle(pageParams);
    }

    /**
     * 首页 最热文章
     * @return
     */
    @PostMapping("hot")
    @Cache(expire = 5 * 60 * 1000,name = "hot_article")
    public Result hotArticle(){
        int limit = 5;
        return articleService.hotArticle(limit);
    }

    /**
     * 首页 最新文章
     * @return
     */
    @PostMapping("new")
    @Cache(expire = 5 * 60 * 1000,name = "news_article")
    public Result newArticles(){
        int limit = 5;
        return articleService.newArticles(limit);
    }

    /**
     * 首页 最新文章
     * @return
     */
    @PostMapping("listArchives")
    public Result listArchives(){
        return articleService.listArchives();
    }


    @PostMapping("view/{id}")
    public Result findArticleById(@PathVariable("id") Long articleId){
        return articleService.findArticleById(articleId);
    }
    //接口url：/articles/publish
    //
    //请求方式：POST
    @PostMapping("publish")
    public Result publish(@RequestBody ArticleParam articleParam){

        return articleService.publish(articleParam);
    }
    @PostMapping("delete")
    public Result delete(@RequestParam("id") Long id) {

//        if (articleService.delete(id) > 0) {
//            System.out.println("--------------------cg");
//        } else {
//            System.out.println("----------------------shibai");
//        }
//        Result result = new Result();
        Article oldArticle = articleMapper.selectById(id);
        oldArticle.setWeight(1);
        int res = articleService.deleteArticle(oldArticle);
//        articleService.
        if (res == 0) {
            return Result.fail(50005, "删除失败");
        } else {
            /**
             * 1. 根据id查询 文章信息
             * 2. 根据bodyId和categoryid 去做关联查询
             */
            Article article = articleMapper.selectById(id);
            ArticleVo articleVo =articleService.copy(article, true, true,true,true);
            return Result.success(articleVo);
        }
    }

    @DeleteMapping("drop")
    public Result drop(@RequestParam("id") Long id){
        int res = articleService.dropArticle(id);
        if (res == 0) {
            return Result.fail(50005, "删除失败");
        } else {
            System.out.println(res);
            return Result.success("成功");
        }

    }

}
